package com.qf.Dao.Impl;/*
 *
 */

import com.qf.Dao.OrderDao;
import com.qf.Domain.Order;
import com.qf.Domain.OrderDetail;
import com.qf.Utils.DataSourceUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class OrderDaoImpl implements OrderDao {
    private QueryRunner qr=new QueryRunner();
    @Override
    public void insert(Order order) {
        Connection conn = DataSourceUtils.getConnection();
        String sql="insert into tb_order(id,uid,money,status,time,aid)values(?,?,?,?,?,?);";
        Object[] params={order.getId(),order.getUid(),order.getMoney(),order.getStatus(),order.getTime(),order.getAid()};
        try {
            qr.update(conn, sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public void insertDetail(OrderDetail orderDetail) {
        Connection conn = DataSourceUtils.getConnection();
        String sql="insert into tb_orderdetail(oid,pid,num,money)values(?,?,?,?);";
        Object[] params={orderDetail.getOid(),orderDetail.getPid(),orderDetail.getNum(),orderDetail.getMoney()};
        try {
            qr.update(conn, sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public void update(String oid, String status) {
        Connection conn = DataSourceUtils.getConnection();
        String sql="update tb_order set status=? where id=?";
        try {
            qr.update(conn, sql,status,oid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public List<Order> selectByOid(Integer uid) {
        Connection conn = DataSourceUtils.getConnection();
        String sql="select o.*,a.detail,a.name,a.phone from tb_order as o inner join tb_address as a on o.aid=a.id where o.uid=?" ;
        try {
            return qr.query(conn, sql,new BeanListHandler<>(Order.class),uid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public Order selectById(String oid) {
        Connection conn = DataSourceUtils.getConnection();
        String sql="select o.*,a.detail,a.name,a.phone from tb_order as o inner join tb_address as a on o.aid=a.id where o.id=?" ;
        try {
            return qr.query(conn, sql,new BeanHandler<>(Order.class),oid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }

    @Override
    public List<OrderDetail> selectDetail(String oid) {
        Connection conn = DataSourceUtils.getConnection();
        String sql="select * from tb_orderdetail where oid=?" ;
        try {
            return qr.query(conn, sql,new BeanListHandler<>(OrderDetail.class),oid);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }finally {
            DataSourceUtils.closeAll(conn, null, null);
        }
    }
}
